package com.focus.xyz.union.pages.frontend.forms;

import com.focus.xyz.core.common.generator.DriverFactory;
import com.focus.xyz.core.common.keyword.Action;
import com.focus.xyz.core.pages.BaseForm;
import com.focus.xyz.core.utils.Constants;
import com.focus.xyz.core.utils.SikuliUtil;
import com.focus.xyz.core.utils.prop.ConfigPropInfo;
import com.focus.xyz.union.data.InsDataService;
import com.focus.xyz.union.data.InsPlanBean;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindAll;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by chenmeng on 2016/4/20.
 */
public class AppInputForm extends BaseForm {
    private Action action;
    private static final String NODE_XPATH = "tbody/tr/td[not(contains(@class,'fn-hide'))]/select|tbody/tr/td[not(contains(@class,'fn-hide'))]/input[@type!='hidden']";
    private static final String url = "http://union.xyz.cn/unionapp/inputAppFrom";

    @FindBy(id = "dev_form")
    private WebElement appForm;
    @FindBy(id = "dev_plan")
    private WebElement insPlanInfo;
    @FindBy(xpath = "//div[@class='ins-info fn-clear']")
    private WebElement beibaorenInfo;
    @FindBy(id = "dev_insappdto_table")
    private WebElement toubaorenInfo;
    @FindBy(id = "dev_submit_button")
    private WebElement submitOrderBtn;
    @FindBy(id = "dev_viewapp")
    private WebElement viewBtn;
    @FindBy(id = "dev_saveapp")
    private WebElement saveBtn;
    @FindBy(id = "dev_agreeCheckBox")
    private WebElement agreeCheckBox;

    @FindBy(id = "dev_insuFillType1")
    private WebElement uploadInsuredLink;
    @FindBy(id = "dev_insuFillType0")
    private WebElement inputInsuredLink;

    @FindAll({
            @FindBy(id = "dev_plan"),
            @FindBy(id = "dev_insappdto_table")
    })
    private List<WebElement> allInsInfo;

    /*
    * 上传模板
    * */
    @FindBy(xpath = "//div[@id='uploadControl']/a[text()='上传']")
    private WebElement uploadBtn;
    /*
    * 投保计划-团体
    * */
    @FindBy(id = "dataOfStartInsureBirthday")
    private WebElement startTime;
    @FindBy(id = "dataOfEndInsure")
    private WebElement endTime;
    @FindBy(id = "dev_period")
    private WebElement period;


    /*
    * 团体险种 公司信息
    * */
    @FindBy(id = "dev_comName")
    private WebElement comName;
    @FindBy(id = "dev_comTel")
    private WebElement comPhone;
    @FindBy(id = "app_province")
    private WebElement comProvince;
    @FindBy(id = "app_city")
    private WebElement comCity;
    @FindBy(id = "dev_address")
    private WebElement comAddress;
    @FindBy(id = "dev_certificateType")
    private WebElement comNoType;
    @FindBy(id = "dev_code")
    private WebElement comNo;

    /*
    * 团体投保按钮
    * */
    @FindBy(id = "dev_submit")
    private WebElement submitBtn;
    @FindBy(xpath = "//td[@id='dev_ensure_btn1']/a")
    private WebElement insuredContent;
    @FindBy(xpath = "//a[contains(@class,'dev_ensure_pop_ok1')]")
    private WebElement insuredContentOk;
    @FindBy(className = "dev_ensure_pop_kill1")
    private WebElement insuredContentCancel;
    @FindBy(id = "dev_ensure_pop1")
    private WebElement insuredContentPopDiv;

    /*
    * 带健康问卷的产品
    * */
    @FindBy(xpath = "//input[contains(@name,'dev_') and @type='radio' and @value='0']")
    private List<WebElement> noRadios;
    @FindBy(id = "dev_agreeCheckBox")
    private WebElement agreeQuestion;
    @FindBy(id = "dev_submit_button")
    private WebElement confirmAndPayBtn;


    public AppInputForm(WebDriver driver,Action action){
        super(driver,action);
        this.action = action;
        PageFactory.initElements(driver,this);
        switchTo(url);
    }

    @Override
    protected List<WebElement> getAllElements() throws Exception {
        List<WebElement> all = new ArrayList<WebElement>();
        for(WebElement info : allInsInfo){
            all.addAll(info.findElements(By.xpath(NODE_XPATH)));
        }
        for(WebElement e:all){
            System.out.println("private String " +e.getAttribute(Constants.ATTR_ID)+";");
        }
        return all;
    }

    public void clickSubmitBtn() throws Exception{
        action.click(submitOrderBtn);
    }

    public void clickView() throws Exception{
        action.click(viewBtn);
    }

    public void clickSave() throws Exception{
        action.click(saveBtn);
    }
    public void clickAgress() throws Exception{
        action.click(agreeCheckBox);
    }

    public void clickSubmitBtn_tuanti() throws Exception{
        action.click(submitBtn);
    }

    public void toubao(String type) throws Exception{
        clickAgress();
        clickSubmitBtn();
        if(Constants.PROD_TYPE_A.equals(type)){
            action.pause(2000l);
            fillQuestionnaire();
        }
    }

    public void fillQuestionnaire() throws Exception{
        if(noRadios!=null&&noRadios.size()>0){
            for(WebElement e : noRadios){
                action.click(e);
            }
        }
        action.click(agreeQuestion);
        action.click(confirmAndPayBtn);
    }

    public void submitOrder() throws Exception{
        clickAgress();
        clickSubmitBtn_tuanti();
    }

    public void clickUploadInsured() throws Exception{
        action.click(uploadInsuredLink);
    }

    //点击 “填写被保险人”链接
    public void clickInputInsured() throws Exception{
        action.jsClick(inputInsuredLink);
//        action.click(inputInsuredLink);
    }

    public void uploadInsuredInfo(String template) throws Exception{
        /*上传团体险被保人模板*/
        SikuliUtil.flashUpload("flashupload.png","open.png","filepath.png",template);
        action.click(uploadBtn);
        action.waitForAjax();
        action.pause(2000l);
        action.click(insuredContent);
        action.assertStringTrue("确定");
        action.click(insuredContentOk);
    }

    public void addCompanyInfo() throws Exception{
        action.inputText(comName,"测试公司");
        action.inputText(comPhone,"02588889999");
        action.select(comProvince,"江苏省");
        action.pause(1000l);
        action.select(comCity,"南京市");
        action.inputText(comAddress,"天华南路1号");
        action.select(comNoType,"组织机构代码");
        action.inputText(comNo,"69466165-4");

    }

    public void addInsPlan(String insPlanId) throws Exception{
        if(StringUtils.isNotBlank(insPlanId)){
            InsPlanBean insPlanBean = InsDataService.getInsPlanById(insPlanId);
            if(insPlanBean == null){
                throw new Exception("insPlan 为空");
            }
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String dbDate = DriverFactory.getDriver(ConfigPropInfo.DB_TYPE).getSysDate();
            int i = DateUtils.truncatedCompareTo(sdf.parse(dbDate),sdf.parse(insPlanBean.getDev_startTime()),9);
            if(i >= 0){
                insPlanBean.setDev_startTime(DriverFactory.getDriver(ConfigPropInfo.DB_TYPE).getNextDate(1));
            }
            i = DateUtils.truncatedCompareTo(sdf.parse(dbDate),sdf.parse(insPlanBean.getDev_endTime()),9);
            if(i >= 0){
                insPlanBean.setDev_endTime(DriverFactory.getDriver(ConfigPropInfo.DB_TYPE).getNextDate(1));
            }
            action.select(period, insPlanBean.getDev_period());
            action.inputText(startTime,insPlanBean.getDev_startTime());
            action.esc();
        }
    }
}
